package cmd

import (
	"github.com/chromedp/cdproto/network"
	// "github.com/chromedp/chromedp"
	"github.com/spf13/cobra"
	"strings"
)

func init() {
	rootCmd.AddCommand(credentialCmd)
}

var credentialCmd = &cobra.Command{
	Use:              "login",
	Short:            "Make global credential of dp",
	Long:             `Make global credential of dp`,
	TraverseChildren: true,
	Run: func(cmd *cobra.Command, args []string) {
		slog.Infof("in login isDebug=>%v", isDebug)
		slog.Infof("city => %s", cityLookup[cityName])
		// runner := NewRunner()

		NewRunner().
			AddListener(func(resp *network.Response) {
				if strings.Contains(resp.URL, "/queryqrcodestatus") {
					slog.Info("等待扫码登录...")
				}
			}).
			Navigate(`https://account.dianping.com/pclogin`).
			OutputQRCode().
			BlockUtil(func(resp *network.Response) bool {
				success := strings.Contains(resp.URL, "userinfo?uuid")
				if success {
					slog.Info("扫码成功...")
				}
				return success
			}).
			DumpCredential().
			Run()

		// runner.
		// AddTasks(
		// 	chromedp.Tasks{
		// 		getQRCode(),
		//	  runner.block(),
		// 		saveCookies(),
		// 	},
		// ).

		// createCredential()
	},
}

// func createCredential() {
// 	RunTasksWithTimeout(credentialTasks(), 30*time.Second)
// }

// 自定义任务
// func credentialTasks() chromedp.Tasks {
// 	return chromedp.Tasks{
// 		chromedp.Navigate(`https://account.dianping.com/pclogin`),
// 		getQRCode(),

// 		saveCookies(),
// 	}
// }
